import {
  IsArray,
  IsNotEmpty,
  IsNumber,
  IsOptional,
  Length,
  Matches,
  Max,
  Min,
} from 'class-validator';
import { Transform } from 'class-transformer';

export class QueryTagDto {
  @Min(1, { message: '不能小于1' })
  @IsNumber({}, { message: '必须为数字' })
  @Transform((value) => Number(value.value))
  pageNo: number;

  @Max(50, { message: '不能大于50' })
  @Min(1, { message: '不能小于1' })
  @IsNumber({}, { message: '必须为数字' })
  @Transform((value) => Number(value.value))
  pageSize: number;

  @Matches(RegExp('ASC|DESC'), { message: '排序方式错误' })
  @Transform((params) => String(params.value).toUpperCase())
  @IsOptional()
  sort?: 'ASC' | 'DESC' = 'DESC';
}

export class CreateTagDto {
  @Length(1, 20, { message: '标签名称长度为1-20' })
  @IsNotEmpty({ message: '标签名称不能为空' })
  name: string;

  @Length(1, 255, { message: '标签描述长度为1-255' })
  @IsNotEmpty({ message: '标签描述不能为空' })
  description: string;
}

export class UpdateTagDto {
  @Length(1, 20, { message: '标签名称长度为1-20' })
  @IsOptional()
  name: string;

  @Length(1, 255, { message: '标签描述长度为1-255' })
  @IsOptional()
  description: string;

  @Length(1, 50, { message: '标签别名长度为1-50' })
  @IsOptional()
  slug: string;
}

export class DeleteTagDto {
  @IsNumber({}, { message: '元素必须是数字', each: true })
  @IsArray({ message: '必须是数组' })
  ids: number[];
}
